<template>
	<view>
		<view class="content">
			<view class="logo">
				<u-image width="100%" height="300rpx" :src="src"></u-image>
				<view class="logoText">汇鸽</view>
			</view>
			<view>
				<u-form :model="form" ref="uForm">
					<u-form-item label="手机号" prop="phone">
						<u-input v-model="form.phone" />
					</u-form-item>
					<u-form-item label="密码" prop="password">
						<u-input v-model="form.password" type="password" />
					</u-form-item>
					<u-form-item label="验证码" prop="captcha">
						<view class="flex">
							<view class="input"><u-input v-model="form.captcha"/></view>
							<view class="captcha" @click="getCaptcha"><u-image width="100%" height="100rpx" :src="'data:image/png;base64,' + pictureUrl"></u-image></view>
						</view>
					</u-form-item>
				</u-form>
			</view>
			<view class="login">
				<u-button shape="circle" :custom-style="loginButton" @click="login">登录</u-button>
				<u-button shape="circle" :ripple="true" @click="loginByWx" type="success">
					<u-icon name="weixin-fill" size="35" class="icon"></u-icon>微信一键登录
				</u-button>
			</view>
			<!-- <view class="authorText">
				<view class="authorTitle">登录后将获得以下权限</view>
				<view class="_li">· 获得你的公开信息（昵称、头像等）</view>
			</view> -->
		</view>
	</view>
</template>

<script>
	import { getCaptcha } from '@/services/user/index.js'
	export default {
		data() {
			return {
				src: 'https://636c-cloud1-7g12v4aa40e3788a-1306685426.tcb.qcloud.la/logo.png?sign=a0c570bee9d3efa0bb0c1eccff0ccd48&t=1627540877',
				form: {
					phone: "",
					password: "",
					captcha: ""
				},
				loginButton: {
					marginBottom: '30rpx'
				},
				rules: {
					phone: [{
						required: true,
						message: '请输入账号',
						// 可以单个或者同时写两个触发验证方式 
						trigger: ['change', 'blur'],
					}],
					password: [{
						required: true,
						message: '请输入密码',
						trigger: ['change', 'blur'],
					}],
					captcha: [{
						required: true,
						message: '请输入验证码',
						trigger: ['change', 'blur'],
					}]
				},
				pictureUrl: ''
			}
		},
		props: {
			getinfo: {
				type: Function,
				default: null
			}
		},
		// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onReady() {
			this.$refs.uForm.setRules(this.rules);
			this.getCaptcha();
		},
		methods: {
			async getCaptcha() {
				await getCaptcha().then(res => {
					if(res.statusCode === 200) {
						this.pictureUrl = res.data.data
					}
				})
			},
			login() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						this.$emit("getInfo", this.form)
					}
				});
			},
			loginByWx() {
				this.$emit("getInfoByWexin")
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		margin: 0 50rpx;

		.logo {
			width: 250rpx;
			height: 400rpx;
			margin: 50rpx auto;
			text-align: center;

			.logoText {
				font-size: 40rpx;
				font-weight: bold;
			}
		}

		.icon {
			margin-right: 20rpx;
		}
		.login {
			margin-top: 30rpx
		}
		.authorText {
			.authorTitle {
				font-size: 35rpx;
				margin-bottom: 20rpx;
			}

			._li {
				font-size: 25rpx;
				color: #aaa;
			}

			margin-bottom: 40rpx;
		}
		.flex {
			display: flex;
			justify-content: space-between;
			.input {
				width: 200rpx;
			}
			.captcha {
				width: 350rpx;
			}
		}
	}
</style>
